/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.counttimer;

import android.app.Activity;
import android.content.Intent ;
import android.widget.ListView;
import android.widget.ListAdapter ;
import android.widget.ArrayAdapter ;
import android.os.Bundle;
import android.app.TimePickerDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.AdapterView.*;
import android.widget.AdapterView;
import android.app.AlertDialog ;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.util.Vector ;

import com.android.MyDatabase ;

/**
 *
 * @author ksemuldie
 */
public class MainActivity extends Activity {
  public static String VAL_ID_SESSION = "_id_session" ;
  private int delete_session ;
  static final int ACTION_DIALOG_ID = 1;

  @Override
  protected void onResume(){
    super.onResume();

    MyDatabase db=new MyDatabase(getApplicationContext());
    db.open();  //apriamo il db

    Cursor c=db.fetchSession();
    startManagingCursor(c);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter( //semplice adapter per i cursor
                        this,
                        R.layout.sessions, //il layout di ogni riga
                        c,
                        new String[]{MyDatabase.ProfileMetaData.SESSION_NAME_KEY},//queste colonne
                        new int[]{R.id.sessionTv});//in queste views

    ListView list1 = (ListView)this.findViewById(R.id.listview1);

    list1.setLongClickable(true) ;
    list1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
        delete_session = (int)id ;

        showDialog(ACTION_DIALOG_ID);


        return true ;
      }
    }) ;

    list1.setOnItemClickListener(new
      AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View v,int position, long id){
          Intent myIntent = new Intent(v.getContext(), EditSessionActivity.class);
          myIntent.putExtra(MainActivity.VAL_ID_SESSION, (int)id);
          startActivity(myIntent);
          //Toast.makeText(getBaseContext(),position +" - "+id+" – Item Selezionato!",Toast.LENGTH_SHORT).show();
        }
    });

    list1.setAdapter(adapter);

    db.close();
  }

  /** Called when the activity is first created. */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Button NewSession = (Button)findViewById(R.id.new_session);
    //NewSession.setText("New Session");

    NewSession.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        // Perform action on click
        //setContentView(R.layout.session);
        Intent myIntent = new Intent(view.getContext(), NewSessionActivity.class);
        //Intent myIntent = new Intent(this, NewSessionActivity.class);
        startActivity(myIntent);
        //startActivityForResult(myIntent, 0);
      }
    });

    
    //String[] cols = new String[]{"Elemento 1", "Elemento 2", "Elemento 3","Elemento 4"};
    
  }

  @Override
  protected Dialog onCreateDialog(int id) {
      switch (id) {
        case ACTION_DIALOG_ID :
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage(getString(R.string.confirm_delete))
            .setCancelable(false)
            .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                MyDatabase db=new MyDatabase(getApplicationContext());
                db.open();  //apriamo il db

                db.deleteSession(delete_session) ;
                delete_session = 0 ;
                db.close() ;

                Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(myIntent) ;

                Toast.makeText(getBaseContext(),
                  "Session Deleted",
                  Toast.LENGTH_SHORT).show();
              }
            }).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface dialog, int id) {
                delete_session = 0 ;
                dialog.cancel();
              }
            });
          return builder.create();

      }
      return null;
  }
}
